Page({
  data: {
      ShowButton:true,
      list:[
        {
          id:1,
          question:"最近一个月，你感到情绪低落、沮丧或无助的频率是？",
          options:[
            {value:'A',ans:"A. 几乎没有",checked:false},
            {value:'B',ans:"B. 偶尔几天",checked:false},
            {value:'C',ans:"C. 有一半以上的时间",checked:false},
            {value:'D',ans:"D. 几乎每天",checked:false}
          ]
        },
        {
          id:2,
          question:"当遇到困难或压力时，你的应对方式是？",
          options:[
            {value:'A',ans:"A. 能积极寻找解决办法并有效处理",checked:false},
            {value:'B',ans:"B. 会寻求他人帮助和支持",checked:false},
            {value:'C',ans:"C. 暂时逃避，但最终会面对",checked:false},
            {value:'D',ans:"D. 感到无力应对，常常陷入焦虑",checked:false}
          ]
        },
        {
          id:3,
          question:"你的睡眠状况如何？",
          options:[
            {value:'A',ans:"A. 通常睡得很好，醒来精神充沛",checked:false},
            {value:'B',ans:"B. 偶尔有睡眠问题，但影响不大",checked:false},
            {value:'C',ans:"C. 经常入睡困难或夜间易醒",checked:false},
            {value:'D',ans:"D. 长期失眠或睡眠过多，影响日常生活",checked:false}
          ]
        },
        {
          id:4,
          question:"你与他人交往的意愿和感受是？",
          options:[
            {value:'A',ans:"A. 乐于与人交往，享受社交活动",checked:false},
            {value:'B',ans:"B. 有选择地参与社交，感觉尚可",checked:false},
            {value:'C',ans:"C. 不太愿意社交，但勉强应付",checked:false},
            {value:'D',ans:"D. 回避社交，感觉与人相处很累",checked:false}
          ]
        },
        {
          id:5,
          question:"你如何看待自己的价值和未来？",
          options:[
            {value:'A',ans:"A. 对自己基本满意，对未来抱有希望",checked:false},
            {value:'B',ans:"B. 有些自我怀疑，但总体上积极",checked:false},
            {value:'C',ans:"C. 经常感到自己不够好，对未来担忧",checked:false},
            {value:'D',ans:"D. 觉得自己毫无价值，对未来绝望",checked:false}
          ]
        },
      ]
  },
  getAnsweredCount(){
    let count = 0;
    const list = this.data.list;
    for(let i = 0; i < list.length; i++){
      const options = list[i].options;
      for(let j = 0; j < options.length; j++){
        if(options[j].checked){
          count++;
          break;
        }
      }
    }
    return count;
  },
  
  GotoResult(){
    const ct = this.getAnsweredCount();
    if(ct !== 5){
      wx.showToast({
        title: `请回答完所有问题`,
        icon:'none'
      })
    }
    else{
      const answers = [];
      const list = this.data.list;
      for(let i = 0; i < list.length; i++){
        const question = list[i];
        let se = '';
        for(let j = 0; j < question.options.length; j++){
          if(question.options[j].checked){
            se = question.options[j].ans;
            break;
          }
        }
        answers.push({
          answer: se
        });
      }
      const answersStr = JSON.stringify(answers);
      wx.navigateTo({
        url: `/pages/result/result?ans=${answersStr}`,
      })
    }
  },
  onRadioChange(e){
    const questionIndex = e.currentTarget.dataset.questionIndex;
    const list = this.data.list;
    const options = list[questionIndex].options;
    
    for (let i = 0, len = options.length; i < len; i++) {
      options[i].checked = options[i].value === e.detail.value;
    }
    
    this.setData({
      list
    });
  }
})